import registnet.dao.Student;
import registnet.dao.StudentPeer;
import registnet.dao.Curriculum;
import registnet.dao.CurriculumCourse;
import registnet.dao.CurriculumCoursePeer;
import registnet.dao.Course;
import registnet.dao.CoursePeer;
import registnet.dao.PlanOfCourseWork;
import registnet.dao.PlanOfCourseWorkPeer;
import resources.Constants;


String _import = bundle.getProperty("registnet.module.data_management.actions.menu.import");

String _plan_of_coursework = bundle.getProperty("registnet.module.data_management.plan_of_coursework");

String _semester = bundle.getProperty("registnet.module.data_management.macro.plan_of_courseworks.form.semester");
String _year = bundle.getProperty("registnet.module.data_management.macro.plan_of_courseworks.form.year");
String _course = bundle.getProperty("registnet.module.data_management.macro.plan_of_courseworks.form.course");
String _save = bundle.getProperty("registnet.module.data_management.macro.plan_of_courseworks.form.save");

String _go = bundle.getProperty("registnet.module.data_management.macro.plan_of_courseworks.search.go");

String _icon_import = bundle.getProperty("registnet.icons.import");

String _registrar = Constants.REGISTRAR;
String _college = Constants.COLLEGE;
String _unit = Constants.UNIT;


public class SelectEventListener implements EventListener {
	private org.zkoss.zul.Bandbox bandbox;
	private org.zkoss.zul.Listbox listbox;
	public SelectEventListener (Bandbox bandbox, Listbox listbox){
		this.bandbox = bandbox;
		this.listbox = listbox;
	}
	public void onEvent(Event event) throws UiException {
		bandbox.value = listbox.getSelectedItem().getLabel();
		bandbox.closeDropdown();
	}
}

Student student;

void showInfo(String studentNumber){
	student = new StudentPeer().getStudent(studentNumber);
	if (student != null){
		if (_user_type.equals(_registrar)){
			retrieveCourseworks();
		}
		else if (_user_type.equals(_college) && student.getCurriculum().getDegreeCourse().getCollege().getCollegeCode().equals(_office)){
			retrieveCourseworks();
		}
		else
			alert("Student not found");
	}
	else
		alert("Student not found");
}

String[] ahCourses = new CoursePeer().getCourseCodesByRGEPDomain("AH"); 
String[] mstCourses = new CoursePeer().getCourseCodesByRGEPDomain("MST");
String[] sspCourses = new CoursePeer().getCourseCodesByRGEPDomain("SSP");
String[] peCourses = new CoursePeer().getPE2CourseCodes();

void retrieveCourseworks(){

	List courses = new CurriculumCoursePeer().getCurriculumCourses(student.getCurriculum().getCurriculumId());

	contents_win.getFellow("coursework_g").getRows().detach();
	Rows rows = new Rows();
	rows.setParent(contents_win.getFellow("coursework_g"));

	HashMap map = new HashMap();
	HashMap map_ui = new HashMap();
	for(int i=0; i<courses.size(); i++){
		CurriculumCourse cc = courses.get(i);
		Course c = cc.getCourse();	
	if(c.getCourseName().equals("(Must be in Plan Of Study)") || c.getCourseCode().equals("PE 2")){
		if(!map.containsKey(c.getCourseCode())){
			List courseList = new Vector();
			courseList.add(cc);
			map.put(c.getCourseCode(), courseList);
			map_ui.put(c.getCourseCode(), new Vector());
		}
		else{
			map.get(c.getCourseCode()).add(cc);
		}
	}
}

	Iterator iter = map.keySet().iterator();
	while(iter.hasNext()){
		String type = iter.next();
		List courseList = map.get(type);
		Group group = new Group(type);
		group.setParent(contents_win.getFellow("coursework_g").getRows());
		for(int i=0; i<courseList.size(); i++){
			Row row = new Row();
			row.setParent(contents_win.getFellow("coursework_g").getRows());
			CurriculumCourse cc = courseList.get(i);
			if (cc.getCourse().getCourseCode().contains("(AH)")){
				Bandbox bandbox = new Bandbox();
				map_ui.get("GE(AH)").add(bandbox);
				Bandpopup bandpopup = new Bandpopup();
				Listbox listbox = new Listbox();
				listbox.setWidth("200px");
				listbox.setRows(8);
				listbox.addEventListener("onSelect", new SelectEventListener(bandbox, listbox));
				for(int j=0; j<ahCourses.length; j++){
					Listitem item = new Listitem();
					new Listcell(ahCourses[j]).setParent(item);
					item.setParent(listbox);
				}
				listbox.setParent(bandpopup);
				bandpopup.setParent(bandbox);
				bandbox.setParent(row);
			}
			else if (cc.getCourse().getCourseCode().contains("(MST)")){
				Bandbox bandbox = new Bandbox();
				map_ui.get("GE(MST)").add(bandbox);
				Bandpopup bandpopup = new Bandpopup();
				Listbox listbox = new Listbox();
				listbox.setWidth("200px");
				listbox.setRows(8);
				listbox.addEventListener("onSelect", new SelectEventListener(bandbox, listbox));
				for(int j=0; j<mstCourses.length; j++){
					Listitem item = new Listitem();
					new Listcell(mstCourses[j]).setParent(item);
					item.setParent(listbox);
				}
				listbox.setParent(bandpopup);
				bandpopup.setParent(bandbox);
				bandbox.setParent(row);
			}
			else if (cc.getCourse().getCourseCode().contains("(SSP)")){
				Bandbox bandbox = new Bandbox();
				map_ui.get("GE(SSP)").add(bandbox);
				Bandpopup bandpopup = new Bandpopup();
				Listbox listbox = new Listbox();
				listbox.setWidth("200px");
				listbox.setRows(8);
				listbox.addEventListener("onSelect", new SelectEventListener(bandbox, listbox));
				for(int j=0; j<sspCourses.length; j++){
					Listitem item = new Listitem();
					new Listcell(sspCourses[j]).setParent(item);
					item.setParent(listbox);
				}
				listbox.setParent(bandpopup);
				bandpopup.setParent(bandbox);
				bandbox.setParent(row);
			}
			else if (cc.getCourse().getCourseCode().startsWith("PE 2")){
				Bandbox bandbox = new Bandbox();
				map_ui.get("PE 2").add(bandbox);
				Bandpopup bandpopup = new Bandpopup();
				Listbox listbox = new Listbox();
				listbox.setWidth("200px");
				listbox.setRows(8);
				listbox.addEventListener("onSelect", new SelectEventListener(bandbox, listbox));
				for(int j=0; j<peCourses.length; j++){
					Listitem item = new Listitem();
					new Listcell(peCourses[j]).setParent(item);
					item.setParent(listbox);
				}
				listbox.setParent(bandpopup);
				bandpopup.setParent(bandbox);
				bandbox.setParent(row);
			}
			else{
				Textbox textbox = new Textbox();
				map_ui.get(cc.getCourse().getCourseCode()).add(textbox);
				textbox.setParent(row);
			}

			Label semester = new Label(Constants.MAP_SEMESTER_VALUE.get(cc.getSemester()));
			semester.setParent(row);
			Label year = new Label(Constants.MAP_YEAR_VALUE.get(cc.getYear()));
			year.setParent(row);
		}
	}
	
	List courseworks = new PlanOfCourseWorkPeer().getPlanOfCourseWorkByStudent(student.getStudentId());
	for(int i=0; i<courseworks.size(); i++){
		PlanOfCourseWork coursework = courseworks.get(i);
		try{
			map_ui.get(coursework.getType()).get(0).setValue(coursework.getCourse().getCourseCode());
			map_ui.get(coursework.getType()).remove(0);
		}catch(Exception e){}
	}
	
}

void save(){
	List groups = contents_win.getFellow("coursework_g").getRows().getGroups();
	new PlanOfCourseWorkPeer().deletePlanOfCourseWork(student.getStudentId());
	for(int i=0; i<groups.size(); i++){
		List items = groups.get(i).getItems();
		for(int j=0; j<items.size(); j++){
			Row row = items.get(j);
			try{
				new PlanOfCourseWorkPeer().createNewPlanofcoursework(student.getStudentId(), new CoursePeer().getCourseId(row.getChildren().get(0).getText()), Constants.MAP_SEMESTER_KEY.get(row.getChildren().get(1).getValue()), Constants.MAP_YEAR_KEY.get(row.getChildren().get(2).getValue()), groups.get(i).getLabel());
			}catch(Exception e){}
		}
	}
	alert("Save Plan of Coursework successful");
}


void importData(){
	org.zkoss.util.media.Media media = Fileupload.get();
	if(media != null){
		java.io.Reader r = new java.io.BufferedReader(media.getReaderData());
		String line = null;
		StringBuffer buf = new StringBuffer(9999999);
			while ((line = r.readLine()) != null) {
			buf.append(line+"\r\n"); 
		}
		new registnet.dao.PlanOfCourseWorkPeer().importPlanOfCourseWorks(buf.toString());
	}
}